package org.luxor.commons.swagger.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.luxor.commons.core.validator.group.AddGroup;
import org.luxor.commons.core.validator.group.UpdateGroup;
import org.luxor.commons.core.web.controller.BaseRestController;
import org.luxor.commons.core.web.data.R;
import org.luxor.commons.swagger.entity.Info;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 *
 * @author Mark @date 2020-12-29 10:27:12
 */
@RestController
@RequestMapping("system/sysUser")
@Api(tags = "系统用户")
public class ApiController extends BaseRestController {

    @ApiOperation("分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页数", paramType = "query", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页记录数", paramType = "query", required = true, defaultValue = "10"),
            @ApiImplicitParam(name = "orders", value = "排序条件，如：[{\"column\":\"id\",\"asc\":true}]", paramType = "query")
    })
    @PostMapping("/list")
    public R<IPage<Info>> list(@ApiIgnore Page page, @ApiParam(value = "查询条件") Map query) {
        IPage<Info> data = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>();
        return R.ok(data);
    }

    @ApiOperation("详情信息")
    @ApiImplicitParam(name = "userId", value = "主键ID", paramType = "path", required = true)
    @GetMapping("/info/{userId}")
    public R<Info> info(@PathVariable("userId") Long userId) {
        Info sysUser = new Info();
        return R.ok(sysUser);
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    public R<Info> save(@Validated(AddGroup.class) @RequestBody Info sysUser) {
        return R.ok(sysUser);
    }

    @ApiOperation("修改")
    @PutMapping("/update")
    public R<Info> update(@Validated(UpdateGroup.class) @RequestBody Info sysUser) {
        return R.ok(sysUser);
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        return R.ok();
    }


    @ApiOperation("查询列表，查询某部门下的用户列表")
    @GetMapping("/listByDeptId")
    public R<List<Info>> listByDeptId(@RequestParam Long deptId) {
        // 部门成员
        List<Info> deptUserList = new ArrayList<>();
        // 部门主管
        Info deptHead = new Info();
        deptUserList.remove(deptHead);
        // 部门分级管理员
        List<Info> deptLeaders = new ArrayList<>();
        deptUserList.removeAll(deptLeaders);

        Map<String, Object> deptUserAllList = new HashMap<>(3);
        deptUserAllList.put("deptHead", deptHead);
        deptUserAllList.put("deptLeaders", deptLeaders);
        deptUserAllList.put("users", deptUserList);
        return R.ok(deptUserList);
    }
}
